
open Gero;;
open Unix;;
open Bigarray;;
open Thread;;

(*
  let vec1 = ref Gero.create floats32 4;;
  let vec2 = ref Gero.create floats32 4;;
  let vec3 = refGero.create floats32 4;;

  let res2 = ref (Gero.create floats32 4);;

  let calcul veca vecb vecc ctx =
  let res_tmp = Gero.fmaf4_spu veca vecb vecc ctx in
  res2 := res_tmp;
  ;; 


  Gero.set vec1 0 1.00000;;
  Gero.set vec1 1 2.30000;;
  Gero.set vec1 2 3.00000;;
  Gero.set vec1 3 4.40000;;
  Gero.set vec2 0 0.20000;;
  Gero.set vec2 1 3.45000;;
  Gero.set vec2 2 6.00000;;
  Gero.set vec2 3 2.50000;;
  Gero.set vec3 0 9.00000;;
  Gero.set vec3 1 2.20000;;
  Gero.set vec3 2 0.10000;;
  Gero.set vec3 3 5.00000;;
*)

let mlMain n  m=
  Random.self_init();
  let vec1 =  Gero.create floats32 n
  and vec2 =  Gero.create floats32 n
  and vec3 =  Gero.create floats32 n
  and res2 = ref (Gero.create floats32 n) in
    (
      Printf.printf "SPE Utilisables : %d\n" (spe_count_usable_spes ());
      for i = 0 to (n-1) do	
	Gero.set vec1 i (Random.float 32.0);
	Gero.set vec2 i (Random.float 32.0);
	Gero.set vec3 i (Random.float 32.0);
      done;
      
      Printf.printf "Bigarray generated\n";
      flush Pervasives.stdout;
      let t1 = Unix.times () in
	(
	  for i = 0 to m do
	    ( 
	      res2 :=  fmaf4 vec1 vec2 vec3;
	    ); 
	  done;
	  let t2 = Unix.times () in
	    (
	      Printf.printf "SPU FINI\n";
	      flush Pervasives.stdout;
	      let t3 = Unix.times () in
		(
		  for i = 0 to m do 
		    (
		      for i = 0 to (n-1) do
			(
			  Gero.set !res2 i ( (Gero.get vec1 i)*.(Gero.get vec2 i)+.(Gero.get vec3 i));
			)
		      done;
		    )
		  done;
		  let t4 = Unix.times () in
		    (
		      for i=0 to 15 do 
			(
			  Printf.printf "OCaml   vec1[%d] : %f * vec2[%d] : %f + vec3[%d] : %f = a[%d] : %f \n" i (Gero.get vec1 i) i (Gero.get vec2 i) i (Gero.get vec3 i) i (Gero.get !res2 i)
			) 
		      done;
		      Printf.printf "Temps AltiVec: %f\n" ((t2.tms_utime)-.(t1.tms_utime));
		      Printf.printf "Temps OCaml: %f\n" ((t4.tms_utime)-.(t3.tms_utime));
		      flush Pervasives.stdout;
		    ); 
		);
	    );
	)
    );
    
;;



mlMain (int_of_string (Sys.argv.(1))) (int_of_string (Sys.argv.(2)));
;;
